package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.pojo.CntrEntity;
import com.hgxd.entity.pojo.HumHistoryEntity;
import com.hgxd.entity.res.ResHumHistory;
import com.hgxd.entity.vo.HumHistoryVo;
import com.hgxd.result.Result;
import com.hgxd.service.CntrService;
import com.hgxd.service.HumHistoryService;
import com.hgxd.service.HumService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: wtf
 * @Description: 温度计历史记录表
 * @Date: 2022/10/20/11:21
 */
@Api(tags = "温度历史表")
@CrossOrigin
@RestController
@RequestMapping("/api/humHistory")
public class HumHistoryController {

    @Resource(name = "HumHistoryService")
    private HumHistoryService humHistoryService;

    @Resource(name = "CntrService")
    private CntrService cntrService;


    @ApiOperation(value = "根据柜号查询对应柜子三个铜排温度", notes = "必须传参柜号（cntrNo）和时间（格式为yyyy-MM-dd）")
    @OperationLogAnnotation(operModule = "温度历史表模块-根据柜号查询对应柜子三个铜排温度", operType = "查询", operContent = "根据柜号查询对应柜子三个铜排温度")
    @PostMapping("getDataByTimeAndCntrNo")
    public Result<Map<Integer, List<ResHumHistory>>> getDataByTimeAndCntrNo(@RequestBody HumHistoryVo humHistoryVo) {
        //TODO 暂时默认第一个高压柜
//        humHistoryVo.setCntrNo("AH1");

        return Result.ok(humHistoryService.getDataByTimeAndCntrNo(humHistoryVo));
    }


    /**
     * 根据柜号查询最新信息
     *
     * @param cntrNo
     * @return
     */
    @ApiOperation(value = "根据柜号查询最新信息", notes = "必须传参柜号（cntrNo）")
    @OperationLogAnnotation(operModule = "温度历史表模块-根据柜号查询最新信息", operType = "查询", operContent = "根据柜号查询最新信息")
    @GetMapping("getNewHumByCntrNo/{cntrNo}")
    public Result<List<HumHistoryEntity>> getNewHumByCntrNo(@PathVariable String cntrNo) {
        return Result.ok(humHistoryService.getNewHumByCntrNo(cntrNo));
    }


    /**
     * 新增
     */
    @PostMapping("/save")
    public Result<Boolean> insert(@RequestBody HumHistoryEntity humHistoryEntity) {
        humHistoryEntity.setUpdateTime(new Date());
        return Result.ok(humHistoryService.save(humHistoryEntity));
    }

    /**
     * 查询全部柜子最新温度
     */
    @GetMapping("/getAllNewHum")
    @ApiOperation(value = "查询全部柜子最新温度")
    @OperationLogAnnotation(operModule = "温度历史表模块-查询全部柜子最新温度", operType = "查询", operContent = "查询全部柜子最新温度")
    public Result<Map<String, List<HumHistoryEntity>>> findAll() {

        //TODO 如果全查数据过多时会特别占内存
        // 查询多少柜号
//        Set<String> cntrNoSet = humHistoryService.list()
//                .stream().map(HumHistoryEntity::getCntrNo)
//                .collect(Collectors.toSet());
//        List<String> cntrNoList = cntrService.list().stream()
//                .map(CntrEntity::getCntrNo)
//                .collect(Collectors.toList());
//
//        // 循环挨个查并加工为Map
//        HashMap<String, List<HumHistoryEntity>> map = new HashMap<>();
//        for (String cntrNo : cntrNoList) {
//            map.put(cntrNo, humHistoryService.getNewHumByCntrNo(cntrNo));
//        }
//        // 返回
//        return Result.ok(map);
        List<HumHistoryEntity> news = humHistoryService.getNews();
        // 使用 Stream API 分组
        Map<String, List<HumHistoryEntity>> groupedByCntrNo = news.stream()
                .collect(Collectors.groupingBy(HumHistoryEntity::getCntrNo));
        return Result.ok(groupedByCntrNo);
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    public Result<HumHistoryEntity> findOne(@PathVariable Integer id) {
        return Result.ok(humHistoryService.getById(id));
    }

    /**
     * 分页模糊查询
     */
    @GetMapping("/get/page")
    public Result<Page<HumHistoryEntity>> findPage(@RequestParam Integer pageNum,
                                                   @RequestParam Integer pageSize) {
        QueryWrapper<HumHistoryEntity> queryWrapper = new QueryWrapper<>();
        // 倒序查询
        queryWrapper.orderByDesc("id");
        return Result.ok(humHistoryService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}
